const Queue = require('../03.队列的封装/queue.cjs')

module.exports = function game(data, interval) {
    const queue = new Queue()
    // 所有人员入队
    for (let i = 0; i < data.length; i++) {
        queue.enQueue(data[i])
    }

    while (queue.size() > 1) {
        // 当击鼓次数没有结束时
        for (let j = 1; j < interval; j++) {
            // 队首出队，进入队尾
            queue.enQueue(queue.deQueue())
        }
        // 击鼓结束，队首淘汰
        console.log(`${queue.deQueue()} out!`)
    }

    const winner = queue.deQueue()
    console.log(`${winner} is the winner!` );
    return winner
}